# -*- coding: utf-8 -*-
import pendulum
from datetime import timedelta
from airflow.operators.python import PythonOperator
from airflow.exceptions import AirflowSkipException
from .dm_complaints_warning_hi_success import main
from ai_ageing_cusc_hi.dm.dm_complaints_warning_hi import jms_ai_group__dm_complaints_warning_hi

cst = pendulum.timezone('Asia/Shanghai')

class ComplaintsWarningSensor(PythonOperator):

    def pre_execute(self, context):
        day = cst.convert(context['ti'].execution_date) + timedelta(hours=6)

        schedule_date = ['00']

        if day.strftime('%H') in schedule_date:
            print(f'{day.strftime("%H")} in {schedule_date}, should skip')
            super().pre_execute(context)
            raise AirflowSkipException()
        else:
            print(f'{day.strftime("%H")} not in {schedule_date}, run now')
            super().pre_execute(context)


jms_dm__dm_complaints_warning_hi_success = ComplaintsWarningSensor(
    task_id='jms_dm__dm_complaints_warning_hi_success',
    email=['jarl.huang@jtexpress.com','yl_bigdata@yl-scm.com'],
    pool='unlimited_pool',
    python_callable=main,
    op_args=['{{ execution_date | cst_hour }}', '{{ var.json.mysq_sync_status2kafka }}'],
)

jms_dm__dm_complaints_warning_hi_success << [jms_ai_group__dm_complaints_warning_hi]